/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Linq;
using System.Collections.Generic;
using CookComputing.XmlRpc;

namespace Whitemice.ZOGI
{
  public class Contact : Company, IEnterpriseContainer, IProjectContainer, IZOGIStorable
  {
    public Contact() : base()
    {
      SetEntityName("Contact");
      SetVersion(0);
      SetHints(new string[] { "WRITE" });
      IsAccount = false;
      AssistantsName = string.Empty;
      BirthDate = DateTime.MinValue;
      Degree = string.Empty;
      Department = string.Empty;
      Description = string.Empty;
      DisplayName = string.Empty;
      FirstName = string.Empty;
      Gender = "unknown";
      LastName = string.Empty;
      ManagersName = string.Empty;
      MiddleName = string.Empty;
      Occupation = string.Empty;
      Office = string.Empty;
      Salutation = string.Empty;
      Login = string.Empty;
    }
          
    public Contact(IDictionary<string,object> x, int d)
      : base(x, d)
    {
    }
    
    internal override void Take(IDictionary<string,object> x, int d)
    {
      base.Take(x, d);
      IsAccount = AsBoolean(x, "isAccount");
			AssistantsName = AsString(x, "assistantName");
			BirthDate = AsDateTime(x, "birthDate");
			Degree = AsString(x, "degree");
			Department = AsString(x, "department");
			Description = AsString(x, "description");
			DisplayName = AsString(x, "displayName");
			FirstName = AsString(x, "firstName");
			Gender = AsString(x, "gender");
			LastName = AsString(x, "lastName");
			ManagersName = AsString(x, "managersName");
			MiddleName = AsString(x, "middleName");
			Occupation = AsString(x, "occupation");
			Office = AsString(x, "office");
			Salutation = AsString(x, "salutation");
			Login = AsString(x, "login");
    }
    
    public bool IsAccount { get; set; }
    public string AssistantsName { get; set; }
		public DateTime BirthDate{ get; set; } 
		public string Degree{ get; set; }
		public string Department { get; set; }
		public string Description { get; set; }
		public string DisplayName { get; set; }
		public string FirstName { get; set; }
		public string LastName { get; set; }
		public string ManagersName { get; set; }
		public string MiddleName { get; set; }
		public string Occupation { get; set; }
		public string Office { get; set; }
		public string Salutation { get; set; }
		public string Login { get; set; }

		private string gender;
		public string Gender
		{
 			get { return gender; }
 			set 
 			{ 
 				switch(value)
 				{
 					case "male": gender = value; break;
 					case "female": gender = value; break;
 					default: gender = string.Empty; break;
 				}
 			}
		} // end Gender

		public override string EMail1 
		{
		  get { return (GetCompanyValue("email1", string.Empty) as string); }
		  set { SetCompanyValue("email1", value); }
		}
		
		public override bool FirstClass { get { return true; } }
    
    public IEnumerable<int> EnterpriseIds
    {
      get { return this.SubordinatesForKey("_ENTERPRISES"); }
    }

    public void GetEnterprises(MultiResultCallback _callback, object context)
    {
      FetchCommand command = new FetchCommand(EnterpriseIds, 65535, context);
      command.Callback = _callback;
      Server.Connection.RunCommand(command);
    }

    public IEnumerable<int> ProjectIds
    {
      get { return this.SubordinatesForKey("_PROJECTS"); }
    }
    
    public void GetProjects(MultiResultCallback _callback, object context)
    {
      FetchCommand command = new FetchCommand(ProjectIds, 65535, context);
      command.Callback = _callback;
      Server.Connection.RunCommand(command);
    }
    
    public override void Assign(Entity _entity)
    {
      if (_entity is Whitemice.ZOGI.Enterprise)
      {
        // contact assignment
        AddSubordinate("_ENTERPRISES", _entity.ObjectId);
        _entity.AddSubordinate("_CONTACTS", ObjectId);
      } else if (_entity is Whitemice.ZOGI.Project)
				{
			    AddSubordinate("_PROJECTS", _entity.ObjectId);
					_entity.AddSubordinate("_CONTACTS", ObjectId);
				}
    } // end Assign
    
    public override void Unassign(Entity _entity)
    {
      if (_entity is Whitemice.ZOGI.Enterprise)
      {
        // contact assignment 
        RemoveSubordinate("_ENTERPRISES", _entity.ObjectId);            
        _entity.RemoveSubordinate("_CONTACTS", ObjectId);         
      } else if (_entity is Whitemice.ZOGI.Project)
        {
          // project assignment
          RemoveSubordinate("_PROJECTS", _entity.ObjectId);                 
          _entity.RemoveSubordinate("_CONTACTS", ObjectId);             
        } // end if-entity-is-project
    } // end Unassign
		
    public override XmlRpcStruct Encode()
    {
    	XmlRpcStruct rpcStruct;
      XmlRpcStruct[] tmpArray;
      int         count;
    	
    	rpcStruct = base.Encode();
    	rpcStruct.Add("entityName", "Contact");
    	if (ObjectId < 0)
    		rpcStruct.Add("objectId", 0);
    		else rpcStruct.Add("objectId", ObjectId);
  		rpcStruct.Add("assistantName", AssistantsName);
 			//rpcStruct.Add("birthDate", BirthDate);
 			rpcStruct.Add("degree", Degree);
 			rpcStruct.Add("department", Department);
 			rpcStruct.Add("description", Description);
 			rpcStruct.Add("displayName", DisplayName);
 			rpcStruct.Add("firstName", FirstName);
 			rpcStruct.Add("gender", Gender);
 			rpcStruct.Add("lastName", LastName);
 			rpcStruct.Add("managersName", ManagersName);
			rpcStruct.Add("middleName", MiddleName);
 			rpcStruct.Add("occupation", Occupation);
 			rpcStruct.Add("office", Office); 
 			 
 			rpcStruct.Add("salutation", Salutation); 
 			

 			// Encode enterprise assignments 
 			count = 0;
 			tmpArray = new XmlRpcStruct[EnterpriseIds.Count()];
 			foreach(int enterpriseId in EnterpriseIds)
 			{
 			  var tmpStruct = new XmlRpcStruct();
 			  tmpStruct.Add("entityName", "assignment");
 			  tmpStruct.Add("targetEntityName", "Enterprise");
 			  tmpStruct.Add("targetObjectId", enterpriseId);
 			  tmpStruct.Add("sourceObjectId", ObjectId);
 			  tmpStruct.Add("sourceEntityName", EntityName);
 			  tmpArray[count] = tmpStruct;
 			  count++;
 			} // end for-enterpriseId
 			rpcStruct.Add("_ENTERPRISES", tmpArray);
 			// Encode project assignments
 			count = 0;
 			tmpArray = new XmlRpcStruct[ProjectIds.Count()];
 			foreach(int projectId in ProjectIds)
 			{
 			  var tmpStruct = new XmlRpcStruct();
 			  tmpStruct.Add("entityName", "assignment");
 			  tmpStruct.Add("targetEntityName", "Project");
 			  tmpStruct.Add("targetObjectId", projectId);
 			  tmpStruct.Add("sourceObjectId", ObjectId);
 			  tmpStruct.Add("sourceEntityName", EntityName);
 			  tmpArray[count] = tmpStruct;
 			  count++;
 			} // end for-enterpriseId
 			rpcStruct.Add("_PROJECTS", tmpArray);
      // return struct
    	return rpcStruct;
    }		
  }
}
